﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using DAO;
using BUS;

namespace AjaxChatService.Controllers
{
    [HttpHeader("Access-Control-Allow-Origin", "*")]
    public class FriendRequestsController : ApiController
    {
        FriendRequestBUS friendRequestBUS = new FriendRequestBUS();

        // GET api/FriendRequests/5
        public HttpResponseMessage GetFriendRequest(int userId)
        {
            FriendRequest friendRequest = friendRequestBUS.GetFriendRequest(userId);
            if (friendRequest == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            return Request.CreateResponse(HttpStatusCode.OK, friendRequest.User.UserName);
        }

        // POST api/FriendRequests
        public HttpResponseMessage PostFriendRequest(int userId, string friendName)
        {
            if (ModelState.IsValid)
            {
                friendRequestBUS.AddFriendRequest(userId, friendName);

                return Request.CreateResponse(HttpStatusCode.OK);
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // DELETE api/FriendRequests/5
        public HttpResponseMessage PostDeleteFriendRequest(int userId, string friendName, string del)
        {
            if(friendRequestBUS.DeleteFriendRequest(userId, friendName))
                return Request.CreateResponse(HttpStatusCode.OK);

            return Request.CreateResponse(HttpStatusCode.NotFound);
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }
    }
}